/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GenerateECKeysPanel.java
 *
 * Created on 01.02.2009, 15:53:29
 */
package jopensslgui;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JFileChooser;

/**
 *
 * @author uli
 */
public class GenerateECKeysPanel extends javax.swing.JPanel
{

    /** Creates new form GenerateECKeysPanel */
    public GenerateECKeysPanel()
    {
        initComponents();
        initCurveComboBox();
    }

    private void initCurveComboBox()
    {
        try
        {
            Process p = Runtime.getRuntime().exec("openssl ecparam -list_curves");
            p.waitFor();
            BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
            while (r.ready())
            {
                String line = r.readLine();
                Matcher m = curvePattern.matcher(line);
                if (m.find())
                {
                    curveComboBox.addItem(m.group(1));
                }
            }
            curveComboBox.setSelectedItem("prime256v1");
        }
        catch (InterruptedException ex)
        {
            Logger.getLogger(GenerateECKeysPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (IOException ex)
        {
            Logger.getLogger(GenerateECKeysPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        curveLabel = new javax.swing.JLabel();
        curveComboBox = new javax.swing.JComboBox();
        outputFileLabel = new javax.swing.JLabel();
        outputFileField = new javax.swing.JTextField();
        selectParamOutputFileButton = new javax.swing.JButton();
        generateKeyButton = new javax.swing.JButton();

        curveLabel.setText( i18n.getString("GenerateECKeysPanel.curveLabel.text")); // NOI18N

        outputFileLabel.setText( i18n.getString("GenerateECKeysPanel.outputFileLabel.text")); // NOI18N

        selectParamOutputFileButton.setText(i18n.getString("GenerateECKeysPanel.selectParamOutputFileButton.text")); // NOI18N
        selectParamOutputFileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectParamOutputFileButtonselectRandomOutputFileButtonActionPerformed(evt);
            }
        });

        generateKeyButton.setText( i18n.getString("GenerateECKeysPanel.generateKeyButton.text")); // NOI18N
        generateKeyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generateKeyButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(generateKeyButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(curveLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(curveComboBox, 0, 223, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(outputFileLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(outputFileField, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(selectParamOutputFileButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(curveLabel)
                    .addComponent(curveComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(outputFileLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(outputFileField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(selectParamOutputFileButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(generateKeyButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void selectParamOutputFileButtonselectRandomOutputFileButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_selectParamOutputFileButtonselectRandomOutputFileButtonActionPerformed
    {//GEN-HEADEREND:event_selectParamOutputFileButtonselectRandomOutputFileButtonActionPerformed
        fileChooser.setSelectedFile(new File(outputFileField.getText()));
        fileChooser.showSaveDialog(this);
        outputFileField.setText(fileChooser.getSelectedFile().getAbsolutePath());
}//GEN-LAST:event_selectParamOutputFileButtonselectRandomOutputFileButtonActionPerformed

    private void generateKeyButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_generateKeyButtonActionPerformed
    {//GEN-HEADEREND:event_generateKeyButtonActionPerformed
        String cmd = "openssl ecparam -genkey -out " + outputFileField.getText() + " -name " +
                (String) curveComboBox.getSelectedItem();
}//GEN-LAST:event_generateKeyButtonActionPerformed
    private Pattern curvePattern = Pattern.compile(" +([a-z0-9-]+) *:");
    private JFileChooser fileChooser = new JFileChooser();
    private ResourceBundle i18n = ResourceBundle.getBundle("jopensslgui/Bundle"); //NOI18N
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox curveComboBox;
    private javax.swing.JLabel curveLabel;
    private javax.swing.JButton generateKeyButton;
    private javax.swing.JTextField outputFileField;
    private javax.swing.JLabel outputFileLabel;
    private javax.swing.JButton selectParamOutputFileButton;
    // End of variables declaration//GEN-END:variables
}
